package com.xiaomili.halfsugar.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.xiaomili.halfsugar.R;
import com.xiaomili.halfsugar.adapter.SearchProductGridView_Adapter;
import com.xiaomili.halfsugar.bean.ProductBean;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

/**
 * 搜索页面---GridViewItem碎片
 * Created by Administrator on 2016/7/20.
 */
public class SearchProductGridViewFragment extends Fragment {
private static String URL="url";
    private static String INDEX="index";
    private static SearchProductGridViewFragment mSearchProductGridViewFragment;
    private String url;
    private int current;
    private GridView gridView;
    private ProductBean productBean;

    public static SearchProductGridViewFragment newSearchProductGridViewFragment(String url,int currentIndex){
        mSearchProductGridViewFragment = new SearchProductGridViewFragment();
        Bundle bundle = new Bundle();
        bundle.putString(URL,url);
        bundle.putInt(INDEX,currentIndex);
        mSearchProductGridViewFragment.setArguments(bundle);
        return mSearchProductGridViewFragment;
    }
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View inflate = inflater.inflate(R.layout.product_gridview, container, false);
        return inflate;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        gridView = (GridView) view.findViewById(R.id.search_product_gv);
        if (getArguments()!=null) {
            url = (String) getArguments().get(URL);
            current = (int) getArguments().get(INDEX);
        }
        OkHttpClient okHttpClient = new OkHttpClient();
        Request request = new Request.Builder().url(url).build();
        Call call = okHttpClient.newCall(request);
        call.enqueue(new Callback() {
            private ProductBean productBean;

            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String json = response.body().string();
                productBean = new Gson().fromJson(json, ProductBean.class);
                getActivity().runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        SearchProductGridView_Adapter gridViewAdapter = new SearchProductGridView_Adapter(getContext(), productBean, current);
                        gridViewAdapter.notifyDataSetChanged();
                        gridView.setAdapter(gridViewAdapter);
                    }
                });


            }
        });
        //GridView监听
        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Toast.makeText(getContext(), "点击了第"+position+"位置", Toast.LENGTH_SHORT).show();
            }
        });
    }
}
